package JUnit;

import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import protocol.Headers;
import Servlets.WhatsAppConstatnts;

public class Create_Group extends mainTest {
	@Before
	public void setUp() throws Exception {
		_url = "create_group";
	}

	@Test
	public void test403() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains(Headers.STATUS_403));
	}

	@Test
	public void test405() {
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"GET /"
								+ _url
								+ ".jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		assertTrue(response1.contains("HTTP/1.1 " + Headers.STATUS_405));
	}

	@Test
	public void testERROR_929() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String hh = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this
				.runtest(new RunnableClient(
						"localhost",
						9969,
						"POST /create_group.jsp HTTP/1.1\nCookie: "
								+ cookie2
								+ "\n\n"
								+ WhatsAppConstatnts.GROUP_NAME
								+ "=TestGroup1&Users=userToAdd,ufdiufdi,yochailehman2\n$"));
		assertTrue(response1.contains(WhatsAppConstatnts.ERROR_929));
	}

	@Test
	public void testERROR_675() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String hh = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME + "=TestGroup1\n$"));
		assertTrue(response1.contains(WhatsAppConstatnts.ERROR_675));
	}

	@Test
	public void testERROR_511() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String hh = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		String response9 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		assertTrue(response9.contains(WhatsAppConstatnts.ERROR_511));
	}

	@Test
	public void testCreateGroup() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String hh = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd,yochailehman2\n$"));
		assertTrue(response1.contains("Group TestGroup1 Created"));
	}
	
	@Test
	public void testCreateGroupSingleUser() {
		String response5 = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=userToAdd&Phone=123456\n$"));
		String hh = this
				.runtest(new RunnableClient("localhost", 9969,
						"POST /login.jsp HTTP/1.1\n\nUserName=yochailehman2&Phone=0546861622\n$"));
		String cookie2 = this.getCookie(response5);
		String response1 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /create_group.jsp HTTP/1.1\nCookie: " + cookie2 + "\n\n"
						+ WhatsAppConstatnts.GROUP_NAME
						+ "=TestGroup1&Users=userToAdd\n$"));
		assertTrue(response1.contains("Group TestGroup1 Created"));
		
		String response6 = this.runtest(new RunnableClient("localhost", 9969,
				"POST /list.jsp HTTP/1.1\nCookie: " + cookie2
						+ "\n\nList=Groups\n$"));

		assertTrue(response6.contains("TestGroup1: 12345"));
	}
}
